diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 78c4f47b5b..67019cd04e 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -1930,20 +1930,23 @@ class Query(BaseExpression):
         group_by = list(self.select)
         if self.annotation_select:
             for alias, annotation in self.annotation_select.items():
-                signature = inspect.signature(annotation.get_group_by_cols)
-                if 'alias' not in signature.parameters:
-                    annotation_class = annotation.__class__
-                    msg = (
-                        '`alias=None` must be added to the signature of '
-                        '%s.%s.get_group_by_cols().'
-                    ) % (annotation_class.__module__, annotation_class.__qualname__)
-                    warnings.warn(msg, category=RemovedInDjango40Warning)
-                    group_by_cols = annotation.get_group_by_cols()
+                if isinstance(annotation, Subquery):
+                    group_by.append(annotation)
                 else:
-                    if not allow_aliases:
-                        alias = None
-                    group_by_cols = annotation.get_group_by_cols(alias=alias)
-                group_by.extend(group_by_cols)
+                    signature = inspect.signature(annotation.get_group_by_cols)
+                    if 'alias' not in signature.parameters:
+                        annotation_class = annotation.__class__
+                        msg = (
+                            '`alias=None` must be added to the signature of '
+                            '%s.%s.get_group_by_cols().'
+                        ) % (annotation_class.__module__, annotation_class.__qualname__)
+                        warnings.warn(msg, category=RemovedInDjango40Warning)
+                        group_by_cols = annotation.get_group_by_cols()
+                    else:
+                        if not allow_aliases:
+                            alias = None
+                        group_by_cols = annotation.get_group_by_cols(alias=alias)
+                    group_by.extend(group_by_cols)
         self.group_by = tuple(group_by)
 
     def add_select_related(self, fields):
